import {
  ADD_CART,
  INIT_BUYCART,
  REDUCE_CART,
  CLEAR_CART,
  SAVE_SHOPID,
  SAVE_CART_ID_SIG,
  CONFIRM_TASTEADD,
}
from './mutation-types.js'
import {
  setStore, getStore
}
from '../config/mUtils'
export default {
  // 加入购物车
  [ADD_CART](state, {
    shopid,
    food_id,
    food_name,
    food_price
  }) {
    let cart = state.cartList;
    let shop = cart[shopid] = (cart[shopid] || {});
    let item = shop[food_id] = (shop[food_id] || {});
    if (item[food_id]) {
      item[food_id]['num']++;
    } else {
      item[food_id] = {
        "num": 1,
        "id": food_id,
        "name": food_name,
        "price": food_price
      };
    }
    state.cartList = {...cart
    };
    //存入localStorage
    setStore('buyCart', state.cartList);
  },
  // 移出购物车
  [REDUCE_CART](state, {
    shopid,
    food_id,
    food_name,
    food_price,
  }) {
    let cart = state.cartList;
    let shop = cart[shopid] = (cart[shopid] || {});
    let item = shop[food_id] = (shop[food_id] || {});
    if (item && item[food_id]) {
      if (item[food_id]['num'] > 0) {
        item[food_id]['num']--;
        state.cartList = {...cart
        };
        //存入localStorage
        setStore('buyCart', state.cartList);
      } else {
        //商品数量为0，则清空当前商品的信息
        item[food_id] = null;
      }
    }
  },
  //网页初始化时从本地缓存获取购物车数据
  [INIT_BUYCART](state) {
    let initCart = getStore('buyCart');
    if (initCart) {
      state.cartList = JSON.parse(initCart);
    }
  },
  //清空当前商品的购物车信息
  [CLEAR_CART](state, shopid) {
    state.cartList[shopid] = null;
    state.cartList = {...state.cartList
    };
    setStore('buyCart', state.cartList);
  },
  //保存桌号id
  [SAVE_SHOPID](state, shopid) {
    state.shopid = shopid;
  },
  //保存下单后 id 和 sig
  [SAVE_CART_ID_SIG](state, {
    cart_id,
    sig
  }) {
    state.cart_id = cart_id;
    state.sig = sig;
  },
  // 口味增加
  [CONFIRM_TASTEADD](state, {
    shopid,
    foods_id,
    tasteDatas,
  }) {
    let cart = state.cartList;
    console.log(tasteDatas)
      // state.cartList[shopid][foods_id][foods_id]['tasteList'] = []
    if (tasteDatas.length > 0) {
      state.cartList[shopid][foods_id][foods_id]['tasteList'] = tasteDatas || []
    } else {
      delete state.cartList[shopid][foods_id][foods_id]['tasteList']
    }



    // state.cartList[shopid][foods_id][foods_id][foods_id] = tasteDatas
    // console.log(state.cartList)
    // if (tasteDatas.length > 0) {
    //   state.cartList[shopid][food_id][food_id]['tasteList'] = tasteDatas
    // }
    state.cartList = {...cart
    };
    //存入localStorage
    setStore('buyCart', state.cartList);
  },
  // // 口味增加
  // [CONFIRM_TASTEREMOVE](state, {
  //   shopid,
  //   food_id,
  //   item,
  // }) {
  //   let cart = state.cartList;
  //   let tasteArr = []
  //   tasteArr.push(item)
  //   state.cartList[shopid][food_id][food_id]['tasteList'] = tasteArr
  //
  //   // console.log(state.cartList[shopid][food_id][food_id])
  //   // state.cartList = {...cart
  //   // };
  //   // //存入localStorage
  //   // setStore('buyCart', state.cartList);
  // },
};
